

/****************************/
void I2C_Delay(void) //for 1T  STC        delay
{
    uint16 j, i;
    for (j = 0; j < 2; j++)
    {
        for (i = 0; i < 7; i++)
            ;
    }
}

/****************************/
void I2C_Start(void) //start the I2C, SDA High-to-low when SCL is high
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SDA = 0;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}

/****************************/
void I2C_Stop(void) //STOP the I2C, SDA Low-to-high when SCL is high
{
    SDA = 0;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SDA = 1;
    I2C_Delay();
}

/****************************/
void S_ACK(void) //Send ACK (LOW)
{
    SDA = 0;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}

/****************************/
void S_NoACK(void) //Send No ACK (High)
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    SCL = 0;
    I2C_Delay();
}

/****************************/
void I2C_Check_ACK(void) //Check ACK, If F0=0, then right, if F0=1, then error
{
    SDA = 1;
    I2C_Delay();
    SCL = 1;
    I2C_Delay();
    F0 = SDA;
    SCL = 0;
    I2C_Delay();
}

/****************************/
void I2C_WriteAbyte(uint8 dat) //write a byte to I2C
{
    uint8 i;
    i = 8;
    do
    {
        if (dat & 0x80)
            SDA = 1;
        else
            SDA = 0;
        dat <<= 1;
        I2C_Delay();
        SCL = 1;
        I2C_Delay();
        SCL = 0;
        I2C_Delay();
    } while (--i);
}

/**************************************
功能描述：向指定地址写数据
入口参数：uint8 addr, uint8 *p, uint8 number
返回值：无
***************************************/
uint8 I2C_ReadAbyte(void) //read A byte from I2C
{
    uint8 i, dat;
    i = 8;
    SDA = 1;
    do
    {
        SCL = 1;
        I2C_Delay();
        dat <<= 1;
        if (SDA)
            dat++;
        SCL = 0;
        I2C_Delay();
    } while (--i);
    return (dat);
}

/**************************************
功能描述：向指定地址写数据
入口参数：uint8 addr, uint8 *p, uint8 number
返回值：无
***************************************/
void WriteNbyte(uint8 addr, uint8 *p, uint8 number) /*        WordAddress,First Data Address,Byte lenth        */
                                                    //F0=0,right, F0=1,error
{
    I2C_Start();
    I2C_WriteAbyte(SLAW);
    I2C_Check_ACK();
    if (!F0)
    {
        I2C_WriteAbyte(addr);
        I2C_Check_ACK();
        if (!F0)
        {
            do
            {
                I2C_WriteAbyte(*p);
                p++;
                I2C_Check_ACK();
                if (F0)
                    break;
            } while (--number);
        }
    }
    I2C_Stop();
}

/**************************************
功能描述：向指定地址读取数据
入口参数：uint8 addr, uint8 *p, uint8 number
返回值：无
***************************************/
void ReadNbyte(uint8 addr, uint8 *p, uint8 number) /*        WordAddress,First Data Address,Byte lenth        */
{
    I2C_Start();
    I2C_WriteAbyte(SLAW);
    I2C_Check_ACK();
    if (!F0)
    {
        I2C_WriteAbyte(addr);
        I2C_Check_ACK();
        if (!F0)
        {
            I2C_Start();
            I2C_WriteAbyte(SLAR);
            I2C_Check_ACK();
            if (!F0)
            {
                do
                {
                    *p = I2C_ReadAbyte();
                    p++;
                    if (number != 1)
                        S_ACK(); //send ACK
                } while (--number);
                S_NoACK(); //send no ACK
            }
        }
    }
    I2C_Stop();
}

/*********************************END FILE********************************************/
